package com.jarvis.product.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.jarvis.common.utils.R;
import com.jarvis.product.service.ApplePayService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ApplePayServiceImpl implements ApplePayService {
    @Override
    public boolean getAppPay(String verifyResult, String transactionID) {
        JSONObject jsonObject = JSONObject.parseObject(verifyResult);
        String status = jsonObject.getString("status");
        //判断是否验证成功
        if ("0".equals(status)) {
            //app端所提供的收据是有效的,验证成功
            String receipt = jsonObject.getString("receipt");
            JSONObject returnJson = JSONObject.parseObject(receipt);
            String in_app = returnJson.getString("in_app");
            JSONObject in_appJson = JSONObject.parseObject(in_app.substring(1, in_app.length() - 1));
            String transactionId = in_appJson.getString("transaction_id");
            String in_app_ownership_type = in_appJson.getString("in_app_ownership_type");
            //如果验证后的订单号与app端传来的订单号一致并且状态为已支付状态则处理自己的业务
            if (transactionID.equals(transactionId) && "PURCHASED".equals(in_app_ownership_type)) {
                //===================处理自己的业务 ============================
               return true;
            }
        }
        return false;
    }
}
